package com.lzcu.maoyan.maoyanweb.service.impl;

import com.lzcu.maoyan.maoyanweb.mapper.BoxOffice;
import com.lzcu.maoyan.maoyanweb.mapper.MovieDetailedMapper;
import com.lzcu.maoyan.maoyanweb.pojo.MovieBoxOfficePojo;
import com.lzcu.maoyan.maoyanweb.pojo.MovieDetailed;
import com.lzcu.maoyan.maoyanweb.service.MovieEcharts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangLiang
 * @date `2024/4/22`
 */
@Service
public class MovieEchartsImpl implements MovieEcharts {

    @Autowired
    BoxOffice boxOffice;

    @Autowired
    MovieDetailedMapper movieDetailedMapper;

    @Override
    public List<Map<String, Object>> queryMaxTotalSessions() {
        List<Map<String, Object>> result = boxOffice.selectMaxTotalSessions();
        for (Map<String, Object> map : result) {
            String movieName = (String) map.get("MOVIE_NAME");
            MovieDetailed movieDetailed = movieDetailedMapper.selectName(movieName);
            Map<String, Object> info = new HashMap<>();
            if (null != movieDetailed) {
                info.put("电影名", movieDetailed.getMovieName());
                info.put("电影上映时间", movieDetailed.getReleaseDate());
                info.put("电影类型", movieDetailed.getTypeMovie());
                info.put("电影主演", movieDetailed.getActTheLeadingRole());
            }
            map.put("info", info);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> queryMovieCount() {
        List<Map<String, Object>> source = movieDetailedMapper.selectMovieCount();
//        List<Map<String, Object>> result = new ArrayList<>();
//        for (Map<String, Object> item : source) {
//            Map<String, Object> itemMap = new HashMap<>();
//            itemMap.put("date", String.valueOf(item.get("YEAR")) + "年" + String.valueOf(item.get("MONTH")) + "月");
//            itemMap.put("count", item.get("COUNT"));
//            result.add(itemMap);
//        }
//        return result;
        return source;
    }

    @Override
    public List<Map<String, Object>> queryMovieTypeCount() {
        List<Map<String, Object>> result = movieDetailedMapper.selectMovieTypeCount();

        return result;
    }

    @Override
    public List<MovieBoxOfficePojo> queryDailyMovieDetails() {
        List<Map<String, Object>> maxTotalSessions = boxOffice.selectMaxTotalSessions();
        String names = "(";
        for (Map<String, Object> maxTotalSession : maxTotalSessions) {
            names += ("\'" + (String) maxTotalSession.get("MOVIE_NAME") + "\',");
        }
        names = names.substring(0, names.length() - 1);
        // TODO 联动数据集
        List<MovieBoxOfficePojo> movieBoxOfficePojo = boxOffice.selectName("飞驰人生2");
        Map<String, Object> result = new HashMap<>();
        List<Integer> removeIndex = new ArrayList<>();
        for (int i = 0; i < movieBoxOfficePojo.size(); i++) {
            MovieBoxOfficePojo movieBoxOfficePojo1 = movieBoxOfficePojo.get(i);
            float movie_frequency_info = movieBoxOfficePojo1.getUP_DAY_INFO();
            if (movie_frequency_info < 0) {
                removeIndex.add(i);
            }
        }
        if (removeIndex.size() > 0) {
            movieBoxOfficePojo.subList(0, removeIndex.size()).clear();
        }
        return movieBoxOfficePojo;
    }

    @Override
    public List<Map<String, Object>> queryMovieLeadingActor() {
        List<Map<String, Object>> result = movieDetailedMapper.selectMovieLeadingActor();

        return result;
    }

    @Override
    public List<Map<String, Object>> queryMovieTypeTotal() {
        List<Map<String, Object>> result = movieDetailedMapper.selectMovieTypeTotal();

        return result;
    }

    @Override
    public List<Map<String, Object>> querySpringFestivalTop() {
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        List<String> movieNames = new ArrayList<>();
        movieNames.add("热辣滚烫");
        movieNames.add("飞驰人生2");
        movieNames.add("熊出没·逆转时空");
        movieNames.add("第二十条");
        for (int i = 1; i <= 30; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("upDate", String.format("第%s天", i));
            for (int j = 1; j <= movieNames.size(); j++) {
                String movieName = movieNames.get(j - 1);
                List<MovieBoxOfficePojo> movieBoxOfficePojos = boxOffice.selectTopMovie(movieName, i);
                int count = 1;
                for (MovieBoxOfficePojo movie : movieBoxOfficePojos) {
                    item.put(String.format("movie%s-%s", j, count), movie.getTOTAL_SESSIONS());
                    item.put(String.format("movie%s-排片场次", j), movie.getMOVIE_FREQUENCY_INFO());
                    item.put(String.format("movie%s-上座率", j), movie.getPEOPLE_PROPORTION_INFO());
                    count += 1;
                }
            }
            result.add(item);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> querySpringFestival() {
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        List<String> movieNames = new ArrayList<>();
        movieNames.add("热辣滚烫");
        movieNames.add("飞驰人生2");
        movieNames.add("熊出没·逆转时空");
        movieNames.add("第二十条");
        for (int i = 1; i <= 30; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("upDate", String.format("第%s天", i));
            for (int j = 1; j <= movieNames.size(); j++) {
                String movieName = movieNames.get(j - 1);
                List<MovieBoxOfficePojo> movieBoxOfficePojos = boxOffice.selectTopMovie(movieName, i);
                if (null != movieBoxOfficePojos) {
                    MovieBoxOfficePojo movie = movieBoxOfficePojos.get(movieBoxOfficePojos.size() - 1);
                    item.put(String.format("movie%s-排片场次", j), movie.getMOVIE_FREQUENCY_INFO());
                    item.put(String.format("movie%s-上座率", j), movie.getPEOPLE_PROPORTION_INFO());
                }
            }
            result.add(item);
        }
        return result;
    }
}
